<%@page import="java.util.*"%>
<%@page import="Data.*"%>
<%@page import="java.text.*"%>
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>MyStuff  |  Buy Stuff</title>
    <style type="text/css">
        <!--
body {
	background-color: #FFFFFF;
	background-image: url(Images/background_grad_small.gif);
	background-repeat: repeat-x;
	margin-left: 0px;
	margin-top: 0px;
	margin-right: 0px;
	margin-bottom: 0px;
}
        -->
    </style>
    <link href="mystuff_text.css" rel="stylesheet" type="text/css">
    <link href="mystuff_layout.css" rel="stylesheet" type="text/css">
    <script type="text/JavaScript">
<!--
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->
    </script>
</head>
<body onLoad="MM_preloadImages('Images/cancel_transaction_over.gif','Images/button_checkout_over.png','Images/button_clear_cart_over.png')">
<div id="main_container">
    <div id="interface_nav_container">
        <div id="small_logo">
            <img src="Images/logo_small.gif">
        </div>
        <div id="interface_top_swap">
        <img src="Images/top_buy_stuff.jpg"></div>
    </div><!-- /interface_nav_container -->
    <div id="step_cancel_container">
        <div class="step">
            <img src="Images/step_1.gif">
            <img src="Images/step_1_pick_buy.gif">				
        </div>
        <div class="cancel">				
        <a href="#" onMouseOver="MM_swapImage('cancel','','Images/cancel_transaction_over.gif',1)" onMouseOut="MM_swapImgRestore()"><img src="Images/cancel_transaction_off.gif" name="cancel" border="0" id="cancel"></a> </div>
    </div><!-- /step_cancel_container -->
    <div id="buy_boxes">
    <div id="inventory_frame">				
        <table width="410" border="0" cellspacing="0">
            <thead class="inventory_header">
                <tr class="inventory_header">
                    <th width="54"><div align="center">Item#</div></th>
                    <th width="184"><div align="center">Description</div></th>
                    <th width="61"><div align="center">Price</div></th>
                    <th width="43"><div align="center">Qty</div></th>
                    <th width="58">&nbsp;</th>
                </tr>
                
            </thead>
            <tbody>
                <% 
                //Create format for Dollar amounts
                NumberFormat form = NumberFormat.getCurrencyInstance();
                List<ConceptualProductBO> conceptualProductList = (List)session.getAttribute("conceptualProductList");
                
                for (int i = 0; i < conceptualProductList.size(); i++) {
                    ConceptualProductBO conceptualProduct = conceptualProductList.get(i);
                    out.println("<form name=\"form"+ i + "\" action=\"edu.byu.isys413.actions.AddProductToCart.action\" method=\"POST\">");
                    if (i%2 == 0){
                        out.println("<tr>");
                        out.println("<td class=\"item_num\">");
                        out.println(conceptualProduct.getId());
                        out.println("<input type=\"hidden\" name=\"id\" value=\"" + conceptualProduct.getId() + "\">");
                        out.println("</td>");
                        out.println("<td class=\"item_description\">");
                        out.println(conceptualProduct.getDescription());
                        out.println("</td>");
                        out.println("<td class=\"item_price\">");
                        out.println(conceptualProduct.getPrice());
                        out.println("</td>");
                        out.println("<td>");
                        out.println("<input type=\"text\" name=\"quantity\" value=\"1\" size=\"1\">");
                        out.println("</td>");
                        out.println("<td>");
                        out.println("<a href=\"javascript:document.form" + i + ".submit()\"><img src=\"Images/button_add.gif\" border=\"0\" alt=\"Add item to cart\" width=\"58\" height=\"23\"></a>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("</form>");
                    }//if
                    else{
                        out.println("<tr>");
                        out.println("<td bgcolor=\"#F7F7F7\" class=\"item_num\">");
                        out.println(conceptualProduct.getId());
                        out.println("<input type=\"hidden\" name=\"id\" value=\"" + conceptualProduct.getId() + "\">");
                        out.println("</td>");
                        out.println("<td bgcolor=\"#F7F7F7\" class=\"item_description\">");
                        out.println(conceptualProduct.getDescription());
                        out.println("</td>");
                        out.println("<td bgcolor=\"#F7F7F7\" class=\"item_price\">");
                        out.println(conceptualProduct.getPrice());
                        out.println("</td>");
                        out.println("<td bgcolor=\"#F7F7F7\">");
                        out.println("<input type=\"text\" name=\"quantity\" value=\"1\" size=\"1\">");
                        out.println("</td>");
                        out.println("<td bgcolor=\"#F7F7F7\">");
                        out.println("<a href=\"javascript:document.form" + i + ".submit()\"><img src=\"Images/button_add.gif\" border=\"0\" alt=\"Add item to cart\" width=\"58\" height=\"23\"></a>");
                        out.println("</td>");
                        out.println("</tr>");
                        out.println("</form>");
                    }//else
                }//for
                
                %>
                
            </tbody>
        </table>
        
    </div><!-- /inventory_frame -->
    <div id="shopping_cart_container">				
    <div class="cart_header" align="center">
        <img src="Images/myCart.gif">
    </div>
    <div class="cart_items" align="center">
    <%
		TransactionBO transaction = (TransactionBO)session.getAttribute("transaction");
            if(transaction.getTxLines().equals(new ArrayList())){
                out.println("<table>");
				out.println("<tr>");
                out.println("<td class=\"item_num\">");
                out.println("Your Shopping Cart is empty");
                out.println("</td>");
                out.println("</tr>");
				out.println("</table>");
            }else{
                List<TransactionLineBO> transactionLineList = transaction.getTxLines();
                
                for (int i = 0; i < transactionLineList.size(); i++) {
                    TransactionLineBO transactionLine = transactionLineList.get(i);
                    SaleBO sale = (SaleBO)transactionLine.getRevenueSource();
                    
                    out.println("<table width=\"210\" border=\"0\" cellspacing=\"0\" class=\"cart_table_border\">");
					out.println("<tr>");
                    out.println("<form name=\"qty_form_" + i + "\" action=\"edu.byu.isys413.actions.UpdateInventoryLine.action\" method=\"POST\">");
                    out.println("<td width=\"70\" bgcolor=\"#F7F7F7\"><div align=\"center\" class=\"item_price\">");
                    out.println(form.format(transactionLine.getTotalPrice()));
                    out.println("<input type=\"hidden\" name=\"id\" value=\"" + transactionLine.getId() + "\">");
                    out.println("</div>");
					out.println("</td>");
                    out.println("<td width=\"140\" bgcolor=\"#F7F7F7\"><div align=\"right\">");
                    out.println("<input type=\"text\" size=\"1\" maxlength=\"2\" value=\"" + sale.getQuantity() + "\" name=\"quantity\"><a href=\"javascript:document.qty_form_" + i + ".submit()\">[update]</a>");
                    out.println("</form>");
					out.println("</div>");
					out.println("</td>");
					out.println("<td bgcolor=\"#F7F7F7\"><div align=\"right\">");
					out.println("<form action=\"edu.byu.isys413.actions.DeleteInventoryLine.action\" method=\"POST\">");
					out.println("<input type=\"hidden\" name=\"id\" value=\"" + transactionLine.getId() + "\">");
					out.println("<input type=\"image\" src=\"Images/x.gif\" alt=\"Remove Item From Cart\" name=\"" + transactionLine.getId() + "\">");
					out.println("</form>");
					out.println("</div>");
					out.println("</td>");
					out.println("</tr>");
					out.println("<tr>");
                    out.println("<td class=\"item_description\" colspan=\"3\">");
                    out.println(transactionLine.getRevenueSource().getDescription());
                    out.println("</td>");
					out.println("</tr>");
					out.println("</table>");
					out.println("<p>");
                    
                }
				}
	%>		
    </div>
    <div class="cart_controls" align="center">					
        <span class="inventory_header">
		<%

                //Display SubTotal in the Shopping Cart
                out.println("<table width=\"200\">");
				out.println("<tr>");
                out.println("<td align=\"right\">");
                out.println("Subtotal: " + form.format(transaction.getSubtotal()));
                out.println("</td>");
                out.println("</tr>");
                //Display Tax in the Shopping Cart
                out.println("<tr>");
                out.println("<td align=\"right\">");
                out.println("Tax: " + form.format(transaction.getTax()));
                out.println("</td>");
                out.println("</tr>");
                //Display Total in the Shopping Cart
                out.println("<tr>");
                out.println("<td align=\"right\">");
                out.println("Total: " + form.format(transaction.getTotal()));
                out.println("</td>");
                out.println("</tr>");
				out.println("</table>");
            
            %>
		</span>
    </div>
    <div class="cart_buttons" align="center">					
        <a href="#" onMouseOver="MM_swapImage('clear_cart','','Images/button_clear_cart_over.png',1)" onMouseOut="MM_swapImgRestore()"><img src="Images/button_clear_cart_off.gif" name="clear_cart" border="0" id="clear_cart"></a>
    <a href="#" onMouseOver="MM_swapImage('checkout','','Images/button_checkout_over.png',1)" onMouseOut="MM_swapImgRestore()"><img src="Images/button_checkout_off.gif" name="checkout" border="0" id="checkout"></a> </div>
</div><!-- /shopping_cart_container -->
</div><!-- /buy boxes -->
</div><!-- /main_container -->
<font color="red">
    <%
    if(request.getAttribute("parameterNames")!=null){
                    out.println(request.getAttribute("parameterNames"));
    }
    %>
</font>

</body>
</html>